<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use app\admin\model\Answer;
use app\admin\model\Question;
use think\Db;
use app\util\Util;

/*
 * 贷款问答
 */
class QuestionController extends CommonController
{
    /*
     * 问答列表
     */
    public function index()
    {
        $list = Question::field('id, question, status, is_hot, add_time')
            ->order('add_time desc')
            ->paginate(15);

        return view('index', [
            'list' => $list
        ]);
    }

    /*
     * 设置取消热门
     */
    public function toggleHot()
    {
        $id = input('param.id');
        $is_hot = input('param.is_hot');

        $data = [
            'is_hot' => $is_hot,
        ];

        $result = Question::where('id', '=', $id)->update($data);
        if ($result) {
            return Util::ajaxReturn(1, '设置成功', null);
        } else {
            return Util::ajaxReturn(0, '设置失败', null);
        }
    }

    /*
     * 贷款问答回复
     */
    public function answer()
    {
        $question_id = input('param.question_id');

        $row = Question::with('answer')
            ->where('id', '=', $question_id)
            ->field('id, question')
            ->find();

        return view('answer', [
            'question_id' => $question_id,
            'row' => $row,
        ]);
    }

    /*
     * 贷款问答专员回答
     */
    public function addAnswer()
    {
        $question_id = input('param.question_id');

        $row = Question::where('id', '=', $question_id)
            ->field('id, question')
            ->find();

        return view('addanswer', [
            'question_id' => $question_id,
            'row' => $row,
        ]);
    }

    /*
     * 写入贷款问答回复
     */
    public function insertAnswer()
    {
        $question_id = input('post.question_id');
        $content = input('post.content');

        $questionRow = Question::where('id', '=', $question_id)
            ->field('status')
            ->find();

        $adminData = session('adminData', '');

        Db::startTrans();

        $answerData = [
            'question_id' => $question_id,
            'reply_type' => Answer::REPLY_TYPE_1,
            'reply_id' => $adminData['id'],
            'reply_name' => $adminData['username'],
            'content' => $content,
            'add_time' => time(),
        ];
        $answerResult = Answer::insert($answerData);

        $questionResult = false;
        if ($questionRow['status'] == Question::STATUS_0) {
            $questionResult = Question::where('id', '=', $question_id)
                ->update(['status' => Question::STATUS_1]);
        } else {
            $questionResult = true;
        }

        if ($answerResult && $questionResult) {
            Db::commit();

            return Util::ajaxReturn(1, '回答成功', null);
        } else {
            Db::rollback();

            return Util::ajaxReturn(0, '回答失败', null);
        }
    }

    /*
     * 删除回答
     */
    public function delAnswer()
    {
        $id = input('param.id');

        $result = Answer::where('id', '=', $id)->delete();
        if ($result) {
            return Util::ajaxReturn(1, '删除成功', null);
        } else {
            return Util::ajaxReturn(0, '删除失败', null);
        }
    }


}